var PATH_PREFIX ="";
function setPathPrefix(path){
	PATH_PREFIX =  path;
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	//初始化时间控件
	WASP_WIDGET.triggerDateRangeStyle("q-time");
	// 招商财富产品线产品
	WASP_WIDGET.triggerPrdLineSelect("q-subPrdCode");
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
  
   

});


//是否选中(样式切换)
function initChoose(att){
    $('.'+att).on('click', function () {
    	var attr = $(this).attr("class");
    	if(attr.indexOf("fa-square") > -1){
    		$(this).removeClass('fa-square');
    		$(this).addClass('fa-check-square');
		}else{
			$(this).removeClass('fa-check-square');
    		$(this).addClass('fa-square');
		}
	});
}



var TRE_NAV = {
	treNavDetail : function(navBizId){
		var url = PATH_PREFIX + "treNavDetail.do?navBizId="+navBizId;
		window.open(url);
	},
	addTreNav: function(){
		var url = PATH_PREFIX + "addTreNav.do";
		window.open(url);
	},
	updateView: function(navBizId) {
    	window.open(PATH_PREFIX + "updateView.do?navBizId=" + navBizId);  
    },
    deleteTreNav: function(navBizId) {
    	/** 删除 */
		ctools.confirm({title : "确认删除？", text: ""},
		function(isConfirm){
			if(isConfirm){
				$.ajax({
					type : "POST",
					dataType : "json",
					url : PATH_PREFIX + "delete.do",
					cache: false,
					data: {
						'navBizId': navBizId
					},
					success: function(data) {
						if (data.success) {
							swal(data.message, "", "success");
							queryByCondtion(false);
						} else {
							swal(data.message, "", "warning");
						}
					},
					error:function(xhr){
					   sweetAlertAjaxError(xhr);
					}
				});
			}
		});
    }
};

var $navList = $('#treNavList');
var $gridparents = $("#myTabContent");//基于哪个父级元素使jqgrid自适应宽度，主要解决tab标签切换隐藏的列表宽度不对的问题
$(function() {
	
	$navList.jqGrid({   
		url:PATH_PREFIX+'treNavListPage.do',
		mtype:'POST',
		datatype: "json",
		colNames:["净值ID","产品","估值日期","单位净值","额度","总净值","总资产","操作"],
		colModel:[  { name: 'navBizId', index: 'navBizId', hidden: true, key: true },
					{ name: 'subFundSname', index: 'subFundSname', width: 30, resizable: true, sortable: false },       
		            { name: 'assessmentDate', index: 'assessmentDate', width: 30, resizable: true, sortable: false },
		            { name: 'nav', index: 'nav', width: 30, resizable: true, sortable: false },
		            { name: 'totalNav', index: 'totalNav', width: 30, resizable: true, sortable: false },
		            { name: 'totalValue', index: 'totalValue', width: 30, resizable: true, sortable: false },
		            { name: 'totalAsset', index: 'totalAsset', width: 30, resizable: true, sortable: false },
		            { name: 'option', index: 'option', width: 60, resizable: true, sortable: false }
				  ],
		rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:false,
		pager: "#treNavListPage",					
		viewrecords: true,
		hidegrid: false, 
		gridComplete: function(){
			var ids = $navList.jqGrid('getDataIDs');
			for(var i=0;i < ids.length;i++){
				
				var rowId = ids[i];
				var rowData = $navList.jqGrid('getRowData', rowId); 
				var params = {
						'detail': 'prdInfoDetailTemp',
						'update': 'templateUpdateTemp',
						'del': 'templateDeleteTemp',
					};
				var permissionButton = Permission_Button.configPermissionButton(params);
				permissionButton.detail = Permission_Button.clickPermissionButton('prdInfoDetailTemp',"TRE_NAV.treNavDetail('" + rowData.navBizId + "');");
				permissionButton.update = Permission_Button.clickPermissionButton('templateUpdateTemp',"TRE_NAV.updateView('" + rowData.navBizId + "');");
				permissionButton.del = Permission_Button.clickPermissionButton('templateDeleteTemp',"TRE_NAV.deleteTreNav('" + rowData.navBizId + "');");
				$navList.jqGrid('setRowData', ids[i], { option: Permission_Button.linkAllBtn(permissionButton)});
			}	
		}
	});
	$navList.navGrid('#treNavListPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$navList.jqGrid('setFrozenColumns');
	jqGridResize($navList);
	
});

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion(true);
	}
});

// 获取选中的值
function getChosen(element){
	var data = '';
	//遍历
    $('.'+element+'.fa-check-square').each(function(index,element){
    	data += $(element).attr("value")+',';
    });
    if(data.length > 0){
    	data = data.substr(0,data.length-1);
    }
    return data;
}

function queryByCondtion(flag) {
	debugger;
    var subPrdCode = $.trim($("#q-subPrdCode").val());
    var startTime    = $("#q-time").getDateRangeValue()[0]; 
	var endTime    = $("#q-time").getDateRangeValue()[1];
    var postData = $navList.jqGrid("getGridParam", "postData");
	$.extend(postData, {
		'sp[subPrdCode]':subPrdCode,
		'sp[startTime]':startTime,
		'sp[endTime]':endTime
	});
    if (flag) {
    	$navList.trigger("reloadGrid", [{ page: 1 }]); //重新载入Grid表格
    } else {
    	$navList.trigger("reloadGrid"); //重新载入Grid表格
    }
};





